<template>
  <div class="cate">
    <van-search
      shape="round"
      background="#fff"
      placeholder="请输入搜索关键词"
      @click="$router.push('/search')"
    />
    <div class="box">
      <div class="left">
        <van-sidebar v-model="activeKey" @change="changeSide">
          <van-sidebar-item
            :title="item.name"
            v-for="item in slidebarlist"
            :key="item.id"
          />
        </van-sidebar>
      </div>
      <div class="right">
        <div class="right-cate" v-for="item in getlist" :key="item.id">
          <img :src="item.icon" alt="" />
          <p>{{ item.name }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      slidebarlist: [{ name: "所有分类", id: 0 }], //侧边导航,顶级分类
      activeKey: 0,
      alllist: [],
    };
  },
  created() {},
  mounted() {
    this.getcatelist();
  },
  methods: {
    getcatelist() {
      this.$axios
        .post("https://api.it120.cc/small4/shop/goods/category/all")
        .then((res) => {
          console.log(res);
          this.alllist = res.data;
          // console.log(this.alllist)
          //   筛选出顶级分类（pid为0的）
          let tem_arr = res.data.filter((item) => {
            return item.pid == 0;
          });
          this.slidebarlist.push(...tem_arr);
        });
    },
    changeSide(index) {
      console.log(this.activeKey, index);
      console.log(this.slidebarlist[index]);
    },
  },
  computed: {
    getlist() {
      if (this.activeKey == 0) {
        return this.alllist;
      } else {
        var pid = this.slidebarlist[this.activeKey].id;
        console.log(pid);
        return this.alllist.filter((item) => {
          return pid == item.pid;
        });
      }
    },
  },
};
</script>

<style scoped lang="scss">
.cate {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.box {
  width: 100%;
  height: 100%;
  display: flex;
  .left {
    flex: 1;
    .van-sidebar {
      width: 100%;
    }
  }
  .right {
    flex: 3;
    display: flex;
    flex-wrap: wrap;
    text-align: center;
    .right-cate {
      width: 31%;
      margin: 1%;
      img {
        width: 100%;
      }
    }
  }
}
</style>
